package com.example.demo;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tvResult;
    private String currentNumber = "";
    private String operator = "";
    private double firstNumber = 0;
    private boolean isNewOperation = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化显示结果的文本框
        tvResult = findViewById(R.id.tvResult);

        // 为所有按钮设置点击事件监听器
        findViewById(R.id.btn0).setOnClickListener(this);
        findViewById(R.id.btn1).setOnClickListener(this);
        findViewById(R.id.btn2).setOnClickListener(this);
        findViewById(R.id.btn3).setOnClickListener(this);
        findViewById(R.id.btn4).setOnClickListener(this);
        findViewById(R.id.btn5).setOnClickListener(this);
        findViewById(R.id.btn6).setOnClickListener(this);
        findViewById(R.id.btn7).setOnClickListener(this);
        findViewById(R.id.btn8).setOnClickListener(this);
        findViewById(R.id.btn9).setOnClickListener(this);
        findViewById(R.id.btnDot).setOnClickListener(this);
        findViewById(R.id.btnAdd).setOnClickListener(this);
        findViewById(R.id.btnSubtract).setOnClickListener(this);
        findViewById(R.id.btnMultiply).setOnClickListener(this);
        findViewById(R.id.btnDivide).setOnClickListener(this);
        findViewById(R.id.btnEquals).setOnClickListener(this);
        findViewById(R.id.btnClear).setOnClickListener(this);
        findViewById(R.id.btnDelete).setOnClickListener(this);
        findViewById(R.id.btnNegate).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        // 数字按钮
        if (id == R.id.btn0) handleNumber("0");
        else if (id == R.id.btn1) handleNumber("1");
        else if (id == R.id.btn2) handleNumber("2");
        else if (id == R.id.btn3) handleNumber("3");
        else if (id == R.id.btn4) handleNumber("4");
        else if (id == R.id.btn5) handleNumber("5");
        else if (id == R.id.btn6) handleNumber("6");
        else if (id == R.id.btn7) handleNumber("7");
        else if (id == R.id.btn8) handleNumber("8");
        else if (id == R.id.btn9) handleNumber("9");
            // 小数点
        else if (id == R.id.btnDot) handleDot();
            // 运算符
        else if (id == R.id.btnAdd) handleOperator("+");
        else if (id == R.id.btnSubtract) handleOperator("-");
        else if (id == R.id.btnMultiply) handleOperator("×");
        else if (id == R.id.btnDivide) handleOperator("÷");
            // 等于
        else if (id == R.id.btnEquals) calculateResult();
            // 清除
        else if (id == R.id.btnClear) clearAll();
            // 删除
        else if (id == R.id.btnDelete) deleteLastChar();
            // 取反
        else if (id == R.id.btnNegate) negateNumber();
    }

    // 处理数字输入
    private void handleNumber(String number) {
        if (isNewOperation) {
            currentNumber = number;
            isNewOperation = false;
        } else {
            currentNumber += number;
        }
        tvResult.setText(currentNumber);
    }

    // 处理小数点
    private void handleDot() {
        if (isNewOperation) {
            currentNumber = "0.";
            isNewOperation = false;
        } else if (!currentNumber.contains(".")) {
            currentNumber += ".";
        }
        tvResult.setText(currentNumber);
    }

    // 处理运算符
    private void handleOperator(String op) {
        if (!currentNumber.isEmpty()) {
            firstNumber = Double.parseDouble(currentNumber);
            operator = op;
            isNewOperation = true;
        }
    }

    // 计算结果
    private void calculateResult() {
        if (!operator.isEmpty() && !currentNumber.isEmpty()) {
            double secondNumber = Double.parseDouble(currentNumber);
            double result = 0;

            switch (operator) {
                case "+":
                    result = firstNumber + secondNumber;
                    break;
                case "-":
                    result = firstNumber - secondNumber;
                    break;
                case "×":
                    result = firstNumber * secondNumber;
                    break;
                case "÷":
                    if (secondNumber != 0) {
                        result = firstNumber / secondNumber;
                    } else {
                        tvResult.setText("错误");
                        return;
                    }
                    break;
            }

            // 处理整数显示，去掉小数点和后面的0
            if (result == (long) result) {
                currentNumber = String.valueOf((long) result);
            } else {
                currentNumber = String.valueOf(result);
            }

            tvResult.setText(currentNumber);
            operator = "";
            isNewOperation = true;
        }
    }

    // 清除所有内容
    private void clearAll() {
        currentNumber = "";
        operator = "";
        firstNumber = 0;
        isNewOperation = true;
        tvResult.setText("0");
    }

    // 删除最后一个字符
    private void deleteLastChar() {
        if (!currentNumber.isEmpty()) {
            currentNumber = currentNumber.substring(0, currentNumber.length() - 1);
            if (currentNumber.isEmpty()) {
                tvResult.setText("0");
                isNewOperation = true;
            } else {
                tvResult.setText(currentNumber);
            }
        }
    }

    // 数字取反
    private void negateNumber() {
        if (!currentNumber.isEmpty() && !currentNumber.equals("0")) {
            if (currentNumber.startsWith("-")) {
                currentNumber = currentNumber.substring(1);
            } else {
                currentNumber = "-" + currentNumber;
            }
            tvResult.setText(currentNumber);
        }
    }
}
